document.addEventListener('DOMContentLoaded', () => {
    // 获取DOM元素
    const gameBoard = document.querySelector('.game-board');
    const scoreDisplay = document.getElementById('score');
    const timeDisplay = document.getElementById('time');
    const startButton = document.getElementById('start-btn');
    const resetButton = document.getElementById('reset-btn');
    const gameOverScreen = document.getElementById('game-over');
    const finalScoreDisplay = document.getElementById('final-score');
    const playAgainButton = document.getElementById('play-again-btn');
    
    // 游戏配置
    const config = {
        rows: 3,
        cols: 3,
        totalHoles: 9,
        gameTime: 30, // 秒
        minShowTime: 500, // 地鼠最小显示时间
        maxShowTime: 1200, // 地鼠最大显示时间
        minHideTime: 300, // 地鼠最小隐藏时间
        maxHideTime: 1000, // 地鼠最大隐藏时间
    };
    
    // 游戏状态
    let gameState = {
        score: 0,
        timeLeft: config.gameTime,
        isPlaying: false,
        timerId: null,
        moleTimers: [],
    };
    
    // 初始化游戏板
    function initGameBoard() {
        gameBoard.innerHTML = '';
        for (let i = 0; i < config.totalHoles; i++) {
            const hole = document.createElement('div');
            hole.classList.add('hole');
            
            const mole = document.createElement('div');
            mole.classList.add('mole');
            
            hole.appendChild(mole);
            gameBoard.appendChild(hole);
            
            // 添加点击事件
            hole.addEventListener('click', () => handleMoleClick(hole));
        }
    }
    
    // 处理点击地鼠
    function handleMoleClick(hole) {
        if (!gameState.isPlaying) return;
        
        if (hole.classList.contains('active')) {
            // 点击中了
            gameState.score++;
            scoreDisplay.textContent = gameState.score;
            
            // 移除活动状态
            hole.classList.remove('active');
            
            // 添加点击效果
            hole.style.transform = 'scale(0.95)';
            setTimeout(() => {
                hole.style.transform = 'scale(1)';
            }, 100);
        }
    }
    
    // 随机显示地鼠
    function showRandomMole() {
        if (!gameState.isPlaying) return;
        
        const holes = document.querySelectorAll('.hole');
        const randomIndex = Math.floor(Math.random() * holes.length);
        const hole = holes[randomIndex];
        
        // 确保这个洞当前没有地鼠
        if (hole.classList.contains('active')) {
            // 如果已经有地鼠，重新选择
            setTimeout(showRandomMole, 100);
            return;
        }
        
        // 显示地鼠
        hole.classList.add('active');
        
        // 随机显示时间
        const showTime = Math.random() * (config.maxShowTime - config.minShowTime) + config.minShowTime;
        
        // 设置定时器隐藏地鼠
        const hideTimer = setTimeout(() => {
            if (gameState.isPlaying) {
                hole.classList.remove('active');
                
                // 随机延迟后再显示另一个地鼠
                const hideDelay = Math.random() * (config.maxHideTime - config.minHideTime) + config.minHideTime;
                setTimeout(showRandomMole, hideDelay);
            }
        }, showTime);
        
        // 保存定时器以便后续清除
        gameState.moleTimers.push(hideTimer);
    }
    
    // 开始游戏
    function startGame() {
        // 重置游戏状态
        gameState.score = 0;
        gameState.timeLeft = config.gameTime;
        gameState.isPlaying = true;
        
        // 更新显示
        scoreDisplay.textContent = gameState.score;
        timeDisplay.textContent = gameState.timeLeft;
        gameOverScreen.classList.add('hidden');
        startButton.disabled = true;
        startButton.style.opacity = 0.5;
        
        // 开始倒计时
        gameState.timerId = setInterval(() => {
            gameState.timeLeft--;
            timeDisplay.textContent = gameState.timeLeft;
            
            if (gameState.timeLeft <= 0) {
                endGame();
            }
        }, 1000);
        
        // 开始随机显示地鼠
        showRandomMole();
    }
    
    // 结束游戏
    function endGame() {
        gameState.isPlaying = false;
        
        // 清除所有定时器
        clearInterval(gameState.timerId);
        gameState.moleTimers.forEach(timer => clearTimeout(timer));
        gameState.moleTimers = [];
        
        // 隐藏所有地鼠
        document.querySelectorAll('.hole').forEach(hole => {
            hole.classList.remove('active');
        });
        
        // 显示游戏结束界面
        finalScoreDisplay.textContent = gameState.score;
        gameOverScreen.classList.remove('hidden');
        
        // 重置按钮状态
        startButton.disabled = false;
        startButton.style.opacity = 1;
    }
    
    // 重置游戏
    function resetGame() {
        // 停止游戏
        gameState.isPlaying = false;
        
        // 清除定时器
        if (gameState.timerId) {
            clearInterval(gameState.timerId);
        }
        gameState.moleTimers.forEach(timer => clearTimeout(timer));
        gameState.moleTimers = [];
        
        // 重置状态
        gameState.score = 0;
        gameState.timeLeft = config.gameTime;
        
        // 更新显示
        scoreDisplay.textContent = gameState.score;
        timeDisplay.textContent = gameState.timeLeft;
        gameOverScreen.classList.add('hidden');
        
        // 隐藏所有地鼠
        document.querySelectorAll('.hole').forEach(hole => {
            hole.classList.remove('active');
        });
        
        // 重置按钮状态
        startButton.disabled = false;
        startButton.style.opacity = 1;
    }
    
    // 添加事件监听器
    startButton.addEventListener('click', startGame);
    resetButton.addEventListener('click', resetGame);
    playAgainButton.addEventListener('click', () => {
        resetGame();
        startGame();
    });
    
    // 初始化游戏
    initGameBoard();
});